package com.powernode.driver.controller;

import com.powernode.common.annotation.PowerLogin;
import com.powernode.common.result.Result;
import com.powernode.common.util.AuthContextHolder;
import com.powernode.driver.service.DriverService;
import com.powernode.driver.service.OrderService;
import com.powernode.model.form.driver.DriverFaceModelForm;
import com.powernode.model.form.driver.UpdateDriverAuthInfoForm;
import com.powernode.model.vo.driver.DriverAuthInfoVo;
import com.powernode.model.vo.driver.DriverLoginVo;
import com.powernode.model.vo.order.NewOrderDataVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "配送员API接口管理")
@RestController
@RequestMapping(value="/driver")
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverController {

    @Resource
    private DriverService driverService;

    @Resource
    private OrderService orderService;

    @Operation(summary = "配送员登录")
    @GetMapping("/login/{code}")
    public Result<String> login(@PathVariable String code) {
        String token = driverService.login(code);
        System.out.println("token:"+token);
        return Result.ok(token);
    }

    @Operation(summary = "获取配送员登录信息")
    @GetMapping("/getDriverLoginInfo")
    @PowerLogin
    public Result<DriverLoginVo> getDriverLoginInfo() {
        //从ThreadLocal中获取当前登录的配送员Id
        Long driverId = AuthContextHolder.getUserId();
        return Result.ok(driverService.getDriverLoginInfo(driverId));
    }


    @Operation(summary = "获取配送员认证信息")
    @GetMapping("/getDriverAuthInfo")
    @PowerLogin
    public Result<DriverAuthInfoVo> getDriverAuthInfo() {
        //从ThreadLocal中获取当前登录的配送员Id
        Long driverId = AuthContextHolder.getUserId();
        return Result.ok(driverService.getDriverAuthInfo(driverId));
    }

    @Operation(summary = "更新配送员认证信息")
    @PostMapping("/updateDriverAuthInfo")
    @PowerLogin
    public Result<Boolean> updateDriverAuthInfo(@RequestBody UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        updateDriverAuthInfoForm.setDriverId(AuthContextHolder.getUserId());
        return Result.ok(driverService.updateDriverAuthInfo(updateDriverAuthInfoForm));
    }

    @Operation(summary = "创建配送员人脸模型")
    @PostMapping("/creatDriverFaceModel")
    @PowerLogin
    public Result<Boolean> creatDriverFaceModel(@RequestBody DriverFaceModelForm driverFaceModelForm) {
        driverFaceModelForm.setDriverId(AuthContextHolder.getUserId());
        return Result.ok(driverService.createDriverFaceModel(driverFaceModelForm));
    }




    @Operation(summary = "查看配送员当天是否进行人脸识别")
    @GetMapping("/isFaceRecognition")
    @PowerLogin
    public Result<Boolean> isFaceRecognition() {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(driverService.isFaceIdentify(userId));

    }


    @Operation(summary = "配送员人脸识别")
    @PostMapping("/verifyDriverFace")
    @PowerLogin
    public Result<Boolean> verifyDriverFace(@RequestBody DriverFaceModelForm driverFaceModelForm) {
        Long userId = AuthContextHolder.getUserId();
        driverFaceModelForm.setDriverId(userId);
        return Result.ok(driverService.verifyDriverFace(driverFaceModelForm));
    }

    @Operation(summary = "配送员开始接单")
    @GetMapping("/startService")
    @PowerLogin
    public Result<Boolean> startService() {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(driverService.startService(userId));
    }

    @Operation(summary = "配送员停止接单")
    @GetMapping("/stopService")
    @PowerLogin
    public Result<Boolean> stopService() {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(driverService.stopService(userId));
    }

}

